/**
 * 
 */
package code.google.qinglan.xml;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author wxq
 * 
 */
public class XpathSelector {
	private XPath xpath;

	/**
	 * 
	 */
	public XpathSelector() {
		XPathFactory xPathFactory = XPathFactory.newInstance();
		this.xpath = xPathFactory.newXPath();
	}
	
	public NodeList selectNodes(Node node, String xpathStr) {
		return _getXpathNode(node, xpathStr);
	}
	/**
	 * 根据给定的Xpath查询xmlDoc
	 * 
	 * @param document
	 *            指定要查询的xmldoc
	 * @param xpathStr
	 *            Xpath字串
	 * @return 节点集
	 */
	private NodeList _getXpathNode(Node node, String xpathStr) {
		try {
			return (NodeList) this.xpath.evaluate(xpathStr, node,
					XPathConstants.NODESET);
		} catch (XPathExpressionException e) {
			e.printStackTrace();
		}
		return null;
	}
}
